import * as child_process from 'child_process';
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
// ...

export function openHarmonyOSEmulator(HarmonyOSEmulatorPath:string,HarmonyOSEmulator:string){
    // Read the lists.json file
    let listsPath = path.join(HarmonyOSEmulatorPath, 'lists.json');
    let lists = JSON.parse(fs.readFileSync(listsPath, 'utf-8'));
    let emulators = lists.map((item: any) => item.name);  
    vscode.window.showQuickPick(emulators, { placeHolder: 'Select an emulator' }).then(selection => {
        if (!selection) {
            return;
        }
        // Execute the command
        let command = `cd ${HarmonyOSEmulatorPath} && ${HarmonyOSEmulator} -hvd ${selection}`;
        let env = Object.assign({}, process.env, { HarmonyOS_HVD_HOME: process.env.HarmonyOS_HVD_HOME });
        
        child_process.exec(command, { env }, (error, stdout, stderr) => {
            if (error) {
                vscode.window.showErrorMessage(`Error executing command: ${error.message}`);
                return;
            }
        
            vscode.window.showInformationMessage('Emulator opened successfully!');
        });
    });
}

